package com.yf.catcafe.service.impl;

import com.yf.catcafe.mapper.MessagesMapper;
import com.yf.catcafe.mapper.UserMapper;
import com.yf.catcafe.pojo.dto.MessageInfoDto;
import com.yf.catcafe.pojo.enity.Message;
import com.yf.catcafe.service.MessageService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessagesMapper messageMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<Message> getMessages() {
        List<Message> messages=messageMapper.getMessages();
        return messages;
    }

    @Override
    public void insertMessages(MessageInfoDto messageInfoDto) {
        Message message=new Message();
        BeanUtils.copyProperties(messageInfoDto,message);
        Long id = messageInfoDto.getId();
        message.setNickName(userMapper.getById(id).getNickName());
        message.setUserName(userMapper.getById(id).getUsername());
        message.setUserId(messageInfoDto.getId());
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        message.setCreateTime(LocalDateTime.parse(messageInfoDto.getTime(), formatter));
        messageMapper.insertMessage(message);
    }
}
